const api = require('../../api/api.js');
const app = getApp();

Page({
  data: {
    imgUrl: app.globalData.imgUrl,
    current: 0,
    cropProduce: [],
    slaughter: []
  },
  onLoad() {
    let that = this;
    wx.getStorage({
      key: 'userId',
      success(res) {
        let userId = res.data
        that.setData({
          userId
        });
        that.queryCropProduce();
        that.querySlaughter();
      }
    });
  },
  queryCropProduce() {
    let that = this;
    let imgUrl = this.data.imgUrl + 'cropProduceImage/';
    let userId = this.data.userId;
    api.queryCropProduce({
      data: {
        cropProduceId: '',
        harvestId: '',
        goodId: -1,
        account: userId
      },
      success(res) {
        console.log(res.data);
        let cropProduce = res.data.CropProduce;
        cropProduce.map((i) => {
          i.image = imgUrl + i.image;
        });
        that.setData({
          cropProduce
        });
      }
    });
  },
  querySlaughter() {
    let that = this;
    let imgUrl = this.data.imgUrl + 'slaughterImage/';
    let userId = this.data.userId;
    api.querySlaughter({
      data: {
        id: '',
        addLivestockId: '',
        userId
      },
      success(res) {
        console.log(res.data);
        let slaughter = res.data.Slaughter;
        slaughter.map((i) => {
          i.image = imgUrl + i.image;
        });
        that.setData({
          slaughter
        });
      }
    });
  },
  goToSuYuanDetailPage(e) {
    let type = e.currentTarget.dataset.type;
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/suyuan-detail/suyuan-detail?type=${type}&id=${id}`,
    })
  },
  slide(e) {
    this.setData({
      current: e.currentTarget.dataset.index
    });
  },
  change(e) {
    this.setData({
      current: e.detail.current
    });
    this.animation(e.detail.current * 50 + '%');
  },
  animation(index) {
    let animation = wx.createAnimation({
      duration: 400,
      timingFunction: 'ease-in-out',
      delay: 0
    });
    animation.left(index).step();
    this.setData({
      animation: animation.export()
    });
  },
})